﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.StoreManage;
using MINERP.ComControls;
using MINERP.BLL.ComplexOrder;

namespace MINERP.StoreManage
{
    public partial class FormStoreManage : FrmParent
    {
        public FormStoreManage()
        {
            InitializeComponent();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            int changeNum=Convert.ToInt32(textEditEditNum.Text);
            if (DialogResult.OK == MessageBox.Show("确定要入库产品" + currProduct.ProductName + "数量为" + changeNum, "确认", MessageBoxButtons.OK, MessageBoxIcon.Information))
            {
                if (changeNum <= 0)
                {
                    MessageBox.Show("请输入正确数字", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                int rtn = _bll.ChangeStore(currProduct, base.userId, "入库", changeNum);
                if (rtn == 0)
                {
                    MessageBox.Show("入库成功", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    currProduct.Num += changeNum;
                    gridControlImport.RefreshDataSource();
                }
                else
                {
                    MessageBox.Show("入库失败，请稍后再试", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }
        StoreManageBll _bll = new StoreManageBll();
        ComplexOrderBll _coBll = new ComplexOrderBll();
        List<ProductStoreInfo> ListProductStore = new List<ProductStoreInfo>();
        List<ComplexOrderInfo> ListComplexOrderInfo = new List<ComplexOrderInfo>();
        int techid = 0;
        Dictionary<int, string> TechDict = new Dictionary<int, string>();
        private void FormStoreManage_Load(object sender, EventArgs e)
        {

            //测试 

            //base.userId = 1;
            TechDict = base.operates;
            techid = TechDict.Where(d => d.Value.Equals("排单生产")).FirstOrDefault().Key;
            ListProductStore = _bll.GetAllProductStore();
            string selectName = tabControl1.SelectedTab.Name;
            if (selectName.Equals("tabPageImport"))
            {
                gridControlImport.DataSource = ListProductStore;

            }
            else if (selectName.Equals("tabPageOutport"))
            {
                //gridControlOutput.DataSource = ListProductStore;
                gridControlComplex.DataSource = ListComplexOrderInfo=_coBll.GetAllComplexOrder(techid);
            }
            else
            {
                gridControlMark.DataSource = ListProductStore;

            }
            
            //gridControlOutput.RefreshDataSource();
        }
        ProductStoreInfo currProduct = new ProductStoreInfo();
       
        private void gridViewImport_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                currProduct = (ProductStoreInfo)gridViewImport.GetRow(e.RowHandle);
                textBoxProductID.Text = currProduct.ProuctId.ToString();
                labelProductName.Text = currProduct.ProductName;
                label1CurrNum.Text = currProduct.Num.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int changeNum = Convert.ToInt32(textEditOutChangeNum.Text);
            if (DialogResult.OK == MessageBox.Show("确定要出库产品" + currProduct.ProductName + "数量为" + changeNum, "确认", MessageBoxButtons.OK, MessageBoxIcon.Information))
            {
                //if (changeNum <= 0)
                //{
                //    MessageBox.Show("请输入正确数字", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
                //if (currProduct.Num < changeNum)
                //{
                //    MessageBox.Show("产品"+currProduct.ProductName+"库存不足,不能出库", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}

                //int rtn = _bll.ChangeStore(currComplex, base.userId, "出库", -changeNum, techid);
                //if (rtn == 0)
                //{
                //    MessageBox.Show("出库成功", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    currProduct.Num -= changeNum;
                //    gridControlOutput.RefreshDataSource();
                //}
                //else
                //{
                //    MessageBox.Show("出库失败，请稍后再试", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //}


            }
        }
        ComplexOrderInfo currComplex = new ComplexOrderInfo();
        private void gridControlComplex_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                currComplex = (ComplexOrderInfo)gridViewComplex.GetRow(e.RowHandle);
                textBoxoutProductID.Text = currComplex.OrderProductId.ToString();
                labelOutProductname.Text = currComplex.OrderProductName;
                //labelOutCurrNum.Text = currComplex.ProductNum.ToString();
            }
        }

        private void gridViewMark_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                currProduct = (ProductStoreInfo)gridViewMark.GetRow(e.RowHandle);
                textEditPandianid.Text = currProduct.ProuctId.ToString();
                label1Pandianname.Text = currProduct.ProductName;
                labelpandianNum.Text = currProduct.Num.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int changeNum = Convert.ToInt32(textEditPandianChange.Text);
            if (DialogResult.OK == MessageBox.Show("确定要盘点产品" + currProduct.ProductName + "数量为" + changeNum, "确认", MessageBoxButtons.OK, MessageBoxIcon.Information))
            {
                if (changeNum <= 0)
                {
                    MessageBox.Show("请输入正确数字", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                int rtn = _bll.PandianStore(currProduct, base.userId, "盘点", changeNum);
                if (rtn == 0)
                {
                    MessageBox.Show("盘点成功", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    currProduct.Num = changeNum;
                    gridControlMark.RefreshDataSource();
                    
                }
                else
                {
                    MessageBox.Show("盘点失败，请稍后再试", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectName = tabControl1.SelectedTab.Name;
            if (selectName.Equals("tabPageImport"))
            {
                gridControlImport.DataSource = ListProductStore;

            }
            else if (selectName.Equals("tabPageOutport"))
            {
                gridControlComplex.DataSource = _coBll.GetAllComplexOrder(techid );

            }
            else
            {
                gridControlMark.DataSource = ListProductStore;

            }
            //gridControlOutput.RefreshDataSource();
        }
        private void gridViewComplex_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            if (e.RowHandle >= 0)
            {

                e.ChildList = ((ComplexOrderInfo)gridViewComplex.GetRow(e.RowHandle)).SubProductList;
            }
        }

        private void gridViewComplex_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewComplex_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }

        private void gridViewComplex_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "子产品";
        }

        private void textEditmnemonicCode_EditValueChanged(object sender, EventArgs e)
        {
            string str = textEditmnemonicCode.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                gridControlImport.DataSource = ListProductStore;
            }
            else
            {
                 List<ProductStoreInfo> newl=ListProductStore.Where(p => p.mnemonicCode.Contains(str) || p.ProductName.Contains(str)).ToList();
                 gridControlImport.DataSource = newl;
                gridControlImport.RefreshDataSource();

            }
        }

        private void textEdit2_EditValueChanged(object sender, EventArgs e)
        {
            string str = textEdit2.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                gridControlMark.DataSource = ListProductStore;
            }
            else
            {
                List<ProductStoreInfo> newl=ListProductStore.Where(p => p.mnemonicCode.Contains(str) || p.ProductName.Contains(str)).ToList();
                gridControlMark.DataSource = newl;

                gridControlMark.RefreshDataSource();

            }
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
